package com.hkts.naturalgas.curvesettings.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.curvesettings.entity.CurveSettings;
import com.hkts.naturalgas.curvesettings.entity.CurveStateParamVo;
import com.hkts.naturalgas.curvesettings.service.ICurveService;
import org.springframework.web.bind.annotation.*;
import com.hkts.naturalgas.utils.CustomPageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hkts.naturalgas.curvesettings.service.ICurveStateService;
import com.hkts.naturalgas.curvesettings.entity.CurveState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author yck
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/instrumentdata/curveState")
public class CurveStateController {
    private final Logger logger = LoggerFactory.getLogger(CurveStateController.class);

    @Autowired
    public ICurveStateService iCurveStateService;

    @Autowired
    public ICurveService iCurveService;

    /**
     * 分页查询数据
     *
     * @param page       分页信息
     * @param curveState 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getCurveStatePageList")
    public Result<Object> getHontUserList(Page<CurveState> page, CurveState curveState) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iCurveStateService.selectPage(page, curveState)));
    }

    /**
     * 保存和修改公用的
     *
     * @param curveState 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/curveStateSave")
    public Result<Object> curveStateSave(CurveState curveState) {
        iCurveStateService.saveOrUpdate(curveState);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/curveStateGetById")
    public Result<Object> curveStateGetById(String id) {
        CurveState byId = iCurveStateService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/curveStateDelete")
    public Result<Object> hontUserDelete(String id) {
        iCurveStateService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/curveStateBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iCurveStateService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    /**
     * 修改切换时间
     * @param item 时间
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateBatchIds")
    public Result<Object> updateBatchIds(Integer item) {
        List<CurveState> curveStates = iCurveStateService.selectListById();
        for (int i = 0; i < curveStates.size(); i++) {
            curveStates.get(i).setSwitchingTime(item);
        }
        iCurveStateService.updateBatchById(curveStates);
        return ResultUtil.success("修改成功");
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateStatus")
    public Result<Object> updateStatus(@RequestBody CurveStateParamVo curveStateVo) {
        CurveState curveState = curveStateVo.getCurveState();
        QueryWrapper<CurveState> query = Wrappers.query();
        query.lambda().eq(CurveState::getWorkShopId,curveState.getWorkShopId());
        CurveState byId = iCurveStateService.getOne(query);
        switch(curveState.getSystemCurveState()){
            case "6" :
                if (byId.getSystemCurveState().equals("2")){
                    byId.setSystemCurveState(curveState.getSystemCurveState());
                    byId.setSwitchingTime(0);
                   // iCurveService.configuretimePoint(curveState.getWorkShopId());
                    Result<Object> objectResult = iCurveService.configuretimePoint(curveStateVo);
                    if(objectResult.getCode()==200){
                        iCurveStateService.saveOrUpdate(byId);
                    }
                    return objectResult;
                }else {
                    return ResultUtil.error("系统正在运行,请重新操作");
                }
            case "3" :
                if (byId.getSystemCurveState().equals("1")||byId.getSystemCurveState().equals("5")){
                    byId.setSystemCurveState(curveState.getSystemCurveState());
                    byId.setSwitchBeginTime(LocalDateTime.now());
                    byId.setSwitchingTime(curveState.getSwitchingTime());
                    iCurveStateService.saveOrUpdate(byId);
                    return ResultUtil.success("正在切换");
                }else if(byId.getSystemCurveState().equals("3")||byId.getSystemCurveState().equals("4")) {
                    return ResultUtil.error("系统数据正在切换，请勿重复操作！");
                }else if(byId.getSystemCurveState().equals("6")){
                    return ResultUtil.error("系统正在启动,20秒后再重新操作！");
                }else{
                    return ResultUtil.error("未在启动中状态,请启动系统后重新操作！");
                }
            case "5" :
                if (byId.getSystemCurveState().equals("3") || byId.getSystemCurveState().equals("4")){
                    byId.setSystemCurveState(curveState.getSystemCurveState());
                    byId.setSwitchBeginTime(LocalDateTime.now());
                    iCurveStateService.saveOrUpdate(byId);
                    return ResultUtil.success("正在恢复");
                }else {
                    return ResultUtil.error("未在切换中状态,请重新操作");
                }
            case "2" :
                if (byId.getSystemCurveState().equals("2")){
                    return ResultUtil.error("已关闭,请重新操作");
                }else {
                    byId.setSystemCurveState(curveState.getSystemCurveState());
                    byId.setSwitchingTime(0);
                    iCurveStateService.saveOrUpdate(byId);
                    return ResultUtil.success("关闭成功");
                }
            default :
                return ResultUtil.error("操作失败！");
        }
    }
}
